package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.Materials;
import com.ruoyi.system.domain.WarehouseComeDetails;
import com.ruoyi.system.domain.WarehouseOutDetails;
import com.ruoyi.system.domain.vo.WarehouseOutDetailsVo;
import com.ruoyi.system.mapper.MaterialsMapper;
import com.ruoyi.system.mapper.WarehouseOutDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WarehouseOutMapper;
import com.ruoyi.system.domain.WarehouseOut;
import com.ruoyi.system.service.IWarehouseOutService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库表Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-26
 */
@Service
public class WarehouseOutServiceImpl implements IWarehouseOutService 
{
    @Autowired
    private WarehouseOutMapper warehouseOutMapper;

    @Autowired
    private WarehouseOutDetailsMapper warehouseOutDetailsMapper;

    @Autowired
    private MaterialsMapper materialsMapper;

    /**
     * 查询出库表
     * 
     * @param warehouseOutId 出库表主键
     * @return 出库表
     */
    @Override
    public WarehouseOut selectWarehouseOutByWarehouseOutId(String warehouseOutId)
    {
        WarehouseOut warehouseOut = warehouseOutMapper.selectWarehouseOutByWarehouseOutId(warehouseOutId);
        warehouseOut.setWarehouseOutDetailsList(warehouseOutDetailsMapper.selectWarehouseOutDetailsListByWarehouseOutId(warehouseOutId));
        return warehouseOut;
    }

    /**
     * 查询出库表列表
     * 
     * @param warehouseOut 出库表
     * @return 出库表
     */
    @Override
    public List<WarehouseOut> selectWarehouseOutList(WarehouseOut warehouseOut)
    {
        return warehouseOutMapper.selectWarehouseOutList(warehouseOut);
    }

    /**
     * 新增出库表
     * 
     * @param warehouseOut 出库表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWarehouseOut(WarehouseOut warehouseOut)
    {
        warehouseOut.setWarehouseOutId(IdUtils.simpleUUID());
        if (StringUtils.isNotEmpty(warehouseOut.getWarehouseOutDetailsList()) && warehouseOut.getWarehouseOutDetailsList().size() > 0) {
            warehouseOut.getWarehouseOutDetailsList().stream()
                    .map(item -> {
                        item.setWarehouseOutDetailsId(IdUtils.simpleUUID());
                        item.setWarehouseOutId(warehouseOut.getWarehouseOutId());
                        return item;
                    })
                    .collect(Collectors.toList());
            for (WarehouseOutDetails warehouseOutDetails : warehouseOut.getWarehouseOutDetailsList()) {
                Materials materials = new Materials();
                materials.setMaterialsId(warehouseOutDetails.getMaterialsId());
                materials.setMaterialsName(warehouseOutDetails.getMaterialsName());
                materials.setMaterialsCategory(warehouseOutDetails.getMaterialsCategory());
                materials.setMaterialsCount(-warehouseOutDetails.getMaterialsCount());
                materials.setMaterialsPrice(warehouseOutDetails.getMaterialsPrice());
                materials.setMaterialsUnit(warehouseOutDetails.getMaterialsUnit());
                materials.setMaterialsDescribe(warehouseOutDetails.getMaterialsDescribe());
                materials.setMaterialsPicture(warehouseOutDetails.getMaterialsPicture());
                materials.setMaterialsBatch(warehouseOutDetails.getMaterialsBatch());
                materials.setMaterialsBatchNumber(warehouseOutDetails.getMaterialsBatchNumber());
                materials.setStorageLocationId(warehouseOutDetails.getWarehouseOutMaterialsTargetStorageLocation());
                materials.setWarehouseId(warehouseOutDetails.getWarehouseOutMaterialsTargetWarehouse());
                materialsMapper.insertMaterials(materials);
            }
            warehouseOutDetailsMapper.insertBatchWarehouseOutDetailsIds(warehouseOut.getWarehouseOutDetailsList());
        }
        return warehouseOutMapper.insertWarehouseOut(warehouseOut);
    }

    /**
     * 修改出库表
     * 
     * @param warehouseOut 出库表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWarehouseOut(WarehouseOut warehouseOut)
    {
        //获取出库的库存记录
        List<WarehouseOutDetailsVo> warehouseOutDetails = warehouseOutDetailsMapper.selectWarehouseOutDetailsListByWarehouseOutId(warehouseOut.getWarehouseOutId());
        //新增的库存记录id为null
        List<WarehouseOutDetailsVo> collectAdd = warehouseOut.getWarehouseOutDetailsList().stream().filter(n -> StringUtils.isEmpty(n.getWarehouseOutDetailsId())).collect(Collectors.toList());
        //删除的入库记录-对物品的数据进行相加
        List<WarehouseOutDetails> collectDel = warehouseOutDetails.stream().filter(n ->
                warehouseOut.getWarehouseOutDetailsList().stream().filter(k -> StringUtils.isNotEmpty(k.getWarehouseOutDetailsId())).collect(Collectors.toList())
                        .stream().noneMatch(h -> h.getWarehouseOutDetailsId().equals(n.getWarehouseOutDetailsId()))
        ).collect(Collectors.toList());
        //更新的库存记录-对物品表的数量进行修改
        List<WarehouseOutDetails> collectUpdate = warehouseOut.getWarehouseOutDetailsList().stream().filter(k -> StringUtils.isNotEmpty(k.getWarehouseOutDetailsId())).collect(Collectors.toList());


        //对id为null,进行新增-商品数量进行相减
        for (WarehouseOutDetails n : collectAdd) {
            n.setWarehouseOutDetailsId(IdUtils.simpleUUID());
            n.setWarehouseOutId(warehouseOut.getWarehouseOutId());
            Materials materials = new Materials();
            materials.setMaterialsId(IdUtils.simpleUUID());
            materials.setMaterialsName(n.getMaterialsName());
            materials.setMaterialsCategory(n.getMaterialsCategory());
            materials.setMaterialsCount(-n.getMaterialsCount());
            materials.setMaterialsPrice(n.getMaterialsPrice());
            materials.setMaterialsUnit(n.getMaterialsUnit());
            materials.setMaterialsDescribe(n.getMaterialsDescribe());
            materials.setMaterialsPicture(n.getMaterialsPicture());
            materials.setMaterialsBatch(n.getMaterialsBatch());
            materials.setMaterialsBatchNumber(n.getMaterialsBatchNumber());
            materials.setStorageLocationId(n.getWarehouseOutMaterialsTargetStorageLocation());
            materials.setWarehouseId(n.getWarehouseOutMaterialsTargetWarehouse());
            materialsMapper.insertMaterials(materials);
        }
        if (StringUtils.isNotEmpty(collectAdd) && collectAdd.size() > 0) {
            warehouseOutDetailsMapper.insertBatchWarehouseOutDetailsIds(collectAdd);
        }

        //对前端删除的数据进行物品表的数量减少-并删除入库记录
        for (WarehouseOutDetails n : collectDel) {
            Materials materials = new Materials();
            materials.setMaterialsId(IdUtils.simpleUUID());
            materials.setMaterialsName(n.getMaterialsName());
            materials.setMaterialsCategory(n.getMaterialsCategory());
            materials.setMaterialsCount(n.getMaterialsCount());
            materials.setMaterialsPrice(n.getMaterialsPrice());
            materials.setMaterialsUnit(n.getMaterialsUnit());
            materials.setMaterialsDescribe(n.getMaterialsDescribe());
            materials.setMaterialsPicture(n.getMaterialsPicture());
            materials.setMaterialsBatch(n.getMaterialsBatch());
            materials.setMaterialsBatchNumber(n.getMaterialsBatchNumber());
            materials.setStorageLocationId(n.getWarehouseOutMaterialsTargetStorageLocation());
            materials.setWarehouseId(n.getWarehouseOutMaterialsTargetWarehouse());
            materialsMapper.insertMaterials(materials);
        }
        if (StringUtils.isNotEmpty(collectDel) && collectDel.size() > 0) {
            warehouseOutDetailsMapper.deleteWarehouseOutDetailsByWarehouseOutDetailsIds(collectDel.stream().map(WarehouseOutDetails::getWarehouseOutDetailsId).toArray(String[]::new));
        }

        //对需要更新的数据进行物品表的更新
        for (WarehouseOutDetails n : collectUpdate) {
            WarehouseOutDetails warehouseComeDetails1 = warehouseOutDetails.stream().filter(k -> StringUtils.isNotEmpty(k.getWarehouseOutDetailsId()) &&
                    k.getWarehouseOutDetailsId().equals(n.getWarehouseOutDetailsId()))
                    .findFirst().orElse(null);

            Materials materials = new Materials();
            materials.setMaterialsId(IdUtils.simpleUUID());
            materials.setMaterialsName(n.getMaterialsName());
            materials.setMaterialsCategory(n.getMaterialsCategory());
            if (warehouseComeDetails1 != null) {
                materials.setMaterialsCount(-(n.getMaterialsCount() - warehouseComeDetails1.getMaterialsCount()));
            }
            materials.setMaterialsPrice(n.getMaterialsPrice());
            materials.setMaterialsUnit(n.getMaterialsUnit());
            materials.setMaterialsDescribe(n.getMaterialsDescribe());
            materials.setMaterialsPicture(n.getMaterialsPicture());
            materials.setMaterialsBatch(n.getMaterialsBatch());
            materials.setMaterialsBatchNumber(n.getMaterialsBatchNumber());
            materials.setStorageLocationId(n.getWarehouseOutMaterialsTargetStorageLocation());
            materials.setWarehouseId(n.getWarehouseOutMaterialsTargetWarehouse());
            materialsMapper.insertMaterials(materials);
            warehouseOutDetailsMapper.updateWarehouseOutDetails(n);
        }
        return warehouseOutMapper.updateWarehouseOut(warehouseOut);
    }

    /**
     * 批量删除出库表
     * 
     * @param warehouseOutIds 需要删除的出库表主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWarehouseOutByWarehouseOutIds(String[] warehouseOutIds)
    {
        for (String warehouseOutId : warehouseOutIds) {
            List<WarehouseOutDetailsVo> warehouseOutDetails = warehouseOutDetailsMapper.selectWarehouseOutDetailsListByWarehouseOutId(warehouseOutId);
            for (WarehouseOutDetails n : warehouseOutDetails) {
                Materials materials = new Materials();
                materials.setMaterialsId(IdUtils.simpleUUID());
                materials.setMaterialsName(n.getMaterialsName());
                materials.setMaterialsCategory(n.getMaterialsCategory());
                materials.setMaterialsCount(n.getMaterialsCount());
                materials.setMaterialsPrice(n.getMaterialsPrice());
                materials.setMaterialsUnit(n.getMaterialsUnit());
                materials.setMaterialsDescribe(n.getMaterialsDescribe());
                materials.setMaterialsPicture(n.getMaterialsPicture());
                materials.setMaterialsBatch(n.getMaterialsBatch());
                materials.setMaterialsBatchNumber(n.getMaterialsBatchNumber());
                materials.setStorageLocationId(n.getWarehouseOutMaterialsTargetStorageLocation());
                materials.setWarehouseId(n.getWarehouseOutMaterialsTargetWarehouse());
                materialsMapper.insertMaterials(materials);
            }
            warehouseOutDetailsMapper.deleteWarehouseOutDetailsByWarehouseOutDetailsId(warehouseOutId);
        }
        return warehouseOutMapper.deleteWarehouseOutByWarehouseOutIds(warehouseOutIds);
    }

    /**
     * 删除出库表信息
     * 
     * @param warehouseOutId 出库表主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseOutByWarehouseOutId(String warehouseOutId)
    {
        return warehouseOutMapper.deleteWarehouseOutByWarehouseOutId(warehouseOutId);
    }
}
